
//pure state
public class puzzle
{
    //super array
    public boolean solved = false;
    public float highscore = 0;
    public puzzle_piece pp0;
    public puzzle_piece pp1;
    public puzzle_piece pp2;
    public puzzle_piece pp3;
    public puzzle_piece pp4;
    public puzzle_piece pp5;
    public puzzle_piece pp6;
    public puzzle_piece pp7;
    public puzzle_piece pp8;
    public puzzle_piece pp9;
    public puzzle_piece pp10;
    public puzzle_piece pp11;
    public puzzle_piece pp12;
    public puzzle_piece pp13;
    public puzzle_piece pp14;
    public puzzle_piece pp15;
    
    public puzzle()
    {
        
    }
    
    public boolean is_solved()
    {
        //test if puzzle is solved
        return solved;
    }
    
    public boolean solve(puzzle_piece p0, puzzle_piece p1, puzzle_piece p2, puzzle_piece p3, 
    puzzle_piece p4, puzzle_piece p5, puzzle_piece p6, puzzle_piece p7, puzzle_piece p8, 
    puzzle_piece p9, puzzle_piece p10, puzzle_piece p11, puzzle_piece p12, puzzle_piece p13, 
    puzzle_piece p14, puzzle_piece p15)
    {
        //fill the 16 puzzle slots
        pp0 = p0;
        pp1 = p1;
        pp2 = p2;
        pp3 = p3;
        pp4 = p4;
        pp5 = p5;
        pp6 = p6;
        pp7 = p7;
        pp8 = p8;
        pp9 = p9;
        pp10 = p10;
        pp11 = p11;
        pp12 = p12;
        pp13 = p13;
        pp14 = p14;
        pp15 = p15;
        
        int w = 0;
        int c = 0;
        //test all nub connections (24)
        //con 1
        if (!checker(p0.right_nub, p1.left_nub))
        { w++; }
        //con 2
        if (!checker(p1.right_nub, p2.left_nub))
        { w++; }
        //con 3
        if (!checker(p2.right_nub, p3.left_nub))
        { w++; }
        
        //con 4
        if (!checker(p4.top_nub, p0.bottom_nub))
        { w++; }
        //con 5
        if (!checker(p5.top_nub, p1.bottom_nub))
        { w++; }
        //con 6
        if (!checker(p6.top_nub, p2.bottom_nub))
        { w++; }
        //con 7
        if (!checker(p7.top_nub, p3.bottom_nub))
        { w++; }
        
        //con 8
        if (!checker(p4.right_nub, p5.left_nub))
        { w++; }
        //con 9
        if (!checker(p5.right_nub, p6.left_nub))
        { w++; }
        //con 10
        if (!checker(p6.right_nub, p7.left_nub))
        { w++; }
        
        //con 11
        if (!checker(p8.top_nub, p4.bottom_nub))
        { w++; }
        //con 12
        if (!checker(p9.top_nub, p5.bottom_nub))
        { w++; }
        //con 13
        if (!checker(p10.top_nub, p6.bottom_nub))
        { w++; }
        //con 14
        if (!checker(p11.top_nub, p7.bottom_nub))
        { w++; }
        
        //con 15
        if (!checker(p8.right_nub, p9.left_nub))
        { w++; }
        //con 16
        if (!checker(p9.right_nub, p10.left_nub))
        { w++; }
        //con 17
        if (!checker(p10.right_nub, p11.left_nub))
        { w++; }
        
        //con 18
        if (!checker(p12.top_nub, p8.bottom_nub))
        { w++; }
        //con 19
        if (!checker(p13.top_nub, p9.bottom_nub))
        { w++; }
        //con 20
        if (!checker(p14.top_nub, p10.bottom_nub))
        { w++; }
        //con 21
        if (!checker(p15.top_nub, p11.bottom_nub))
        { w++; }
        
        //con 22
        if (!checker(p12.right_nub, p13.left_nub))
        { w++; }
        //con 23
        if (!checker(p13.right_nub, p14.left_nub))
        { w++; }
        //con 24
        if (!checker(p14.right_nub, p15.left_nub))
        { w++; }
        float pr = 100-((float)w /24);
        System.out.println("Percent correct: " + pr);
        if (pr > highscore)
        {
            System.out.println("High score found!");
            highscore = pr;
        }
        if (w != 0) { return false; }
        //debugging
       
        //System.out.println("p0's left nub is "+p0.left_nub);
        //System.out.println("p0's right nub is "+p0.right_nub);
        
        solved = true;
        return true;
        
    }
    //checks whether nubs connect
    public boolean checker(int p0, int p1)
    {
        if (p0 == 1 && p1 == 6 || p0 == 2 && p1 == 5 || p0 == 3 && p1 == 7 || p0 == 4 && p1 == 8)
        {
            return true;
        }
        return false;
    }
    
}
